package com.hongya.article4.configurer;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfigurer {

    //用户数据源
    @Bean(name = "userDataSource")//装配该方法的返回值为userDataSource管理的bean
    @Qualifier("userDataSource")//spring装配bean唯一标识
    @ConfigurationProperties(prefix = "spring.datasource.user")//读取配置文件自定义的前缀
    public DataSource userDataSource(){
        return DataSourceBuilder.create().build();
    }

    //书籍数据源
    @Bean(name = "bookDataSource")//装配该方法的返回值为bookDataSource管理的bean
    @Primary//配置该数据源为主数据源
    @Qualifier("bookDataSource")//spring装配bean唯一标识
    @ConfigurationProperties(prefix = "spring.datasource.book")//读取配置文件自定义的前缀
    public DataSource bookDataSource(){
        return DataSourceBuilder.create().build();
    }
}
